package com.qiangqiang.resoldhouse.service;

/**
 * Description: resoldhouse
 * Create by jeff on 2018-8-14 10:00
 */

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qiangqiang.resoldhouse.dto.NewsDTO;
import com.qiangqiang.resoldhouse.dto.PageDTO;
import com.qiangqiang.resoldhouse.entity.News;
import com.qiangqiang.resoldhouse.entity.NewsSelect;
import com.qiangqiang.resoldhouse.mapper.NewsMapper;
import com.qiangqiang.resoldhouse.util.SnowflakeIdWorker;
import com.qiangqiang.resoldhouse.util.TimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class NewsService {
    @Autowired
    private NewsMapper mapper;

    public List<News> seleteAllAgent() {
        return mapper.selectList(null);
    }

    //通过id查找
    public NewsDTO findNewsById(Long newsId) {
        News news = mapper.selectById(newsId);
        return beanToDTO(news);
    }

    public NewsDTO beanToDTO(News news) {
        NewsDTO dto = new NewsDTO();
        BeanUtils.copyProperties(news, dto);
        dto.setUpdateTime(TimeFormat.converTime(news.getUpdateTime()));
        dto.setCreateTime(TimeFormat.converTime(news.getCreateTime()));
        return dto;
    }
    //添加新闻
    public boolean insertNews(News news){
        news.setNewsId(new SnowflakeIdWorker(1,1).nextId());
        news.setStatus(false);
        news.setCreateTime(Instant.now().toEpochMilli());
        news.setUpdateTime(Instant.now().toEpochMilli());
        return mapper.insert(news) > 0;
    }

    //添加删除新闻by id
    public boolean deleteNews(Long newsId) {
        News news = mapper.selectById(newsId);
        return mapper.deleteById(newsId) > 0;
    }

    //修改新闻审核状态
    public boolean passNews(Long newsId, boolean status) {
        News news = mapper.selectById(newsId);
        news.setUpdateTime(Instant.now().toEpochMilli());
        if (status) {
            news.setStatus(false);
        } else {
            news.setStatus(true);
        }
        return mapper.updateById(news) > 0;
    }

    //新闻分页
    public PageDTO findPage(Integer pageNum, NewsSelect select) {
        PageDTO<NewsDTO> pageDTO = new PageDTO<>();
        Page<NewsDTO> page = new Page<>(pageNum, 8);
        List<NewsDTO> result = mapper.selectPage(page, getWrapper(select)).stream()
                .map(this::beanToDTO).collect(Collectors.toList());
        pageDTO.setPage(page);
        pageDTO.setResult(result);
        return pageDTO;
    }


    public Wrapper<News> getWrapper(NewsSelect select) {
        if (select == null) return null;
        Wrapper<News> wrapper = new EntityWrapper<>();
        boolean first = true, has = false;

        //选状态
        if (select.getStatus() != null) {
            if (has) wrapper = wrapper.andNew();
            has = true;
            first = true;
            for (String c : select.getStatus()) {
                if (!first) wrapper = wrapper.or();
                else first = false;
                wrapper = wrapper.eq("status", c);
            }
        }

       //选类型
        if (select.getType() != null) {
            if (has) wrapper = wrapper.andNew();
            has = true;
            wrapper = wrapper.eq("type", select.getType());
        }
     //标题 模糊查询
        if (select.getTitle() != null) {
            if (has) wrapper = wrapper.andNew();
            has = true;
            wrapper = wrapper.like("title", select.getTitle());
        }


        //选择排序方式
        if (!select.getOrder().equals("default"))
            wrapper = wrapper.orderBy(select.getOrder(), false);
        return wrapper;
    }
}
